package com.atguigu.structures.linkedlist;

import com.atguigu.structures.linkedlist.node.SingleNode;
import com.atguigu.structures.linkedlist.node.DoubleNode;

/**
 * 测试链表
 *
 * @author xr-8
 * @date 2022-07-17
 * @time 0:54
 */
public class TestLinkedList {

    public static void main(String[] args) {
//        testSingleLinkedList();
//        testDoubleLinkedList();
        testCircleSingleLinkedList();
    }

    /**
     * 测试单链表
     */
    public static void testSingleLinkedList() {
        //先创建节点
        SingleNode hero1 = new SingleNode(1, "宋江", "及时雨");
        SingleNode hero2 = new SingleNode(2, "卢俊义", "玉麒麟");
        SingleNode hero3 = new SingleNode(3, "吴用", "智多星");
        SingleNode hero4 = new SingleNode(4, "林冲", "豹子头");

        SingleNode hero5 = new SingleNode(5, "宋江1", "及时雨1");
        SingleNode hero6 = new SingleNode(6, "卢俊义", "玉麒麟1");
        SingleNode hero7 = new SingleNode(7, "吴用1", "智多星1");
        SingleNode hero8 = new SingleNode(8, "林冲1", "豹子头1");
        //创建要给链表
        SingleLinkedList singleLinkedList = new SingleLinkedList();
        //加入
        singleLinkedList.add(hero1);
        singleLinkedList.add(hero4);
        singleLinkedList.add(hero2);
        singleLinkedList.add(hero3);

        // 测试一下单链表的合并
        //创建要给链表
        SingleLinkedList dist = new SingleLinkedList();
        SingleLinkedList src = new SingleLinkedList();
        //加入
        dist.addByOrder(hero1);
        dist.addByOrder(hero4);
        dist.addByOrder(hero2);
        dist.addByOrder(hero3);
        src.addByOrder(hero6);
        src.addByOrder(hero7);
        src.addByOrder(hero5);
        src.addByOrder(hero8);

        dist.list();
        src.list();
        SingleLinkedListUtil.mergeLinkedList(dist, src);
        System.out.println("合并后~~");
        dist.list();

        // 测试一下单链表的反转功能
//        System.out.println("原来链表的情况~~");
//        singleLinkedList.list();
//        System.out.println("反转单链表~~");
//        SingleLinkedListUtil.reverseList(singleLinkedList.getHead());
//        singleLinkedList.list();

//        System.out.println("测试逆序打印单链表, 没有改变链表的结构~~");
//        reversePrint(singleLinkedList.getHead());


        //加入按照编号的顺序
//		singleLinkedList.addByOrder(hero1);
//		singleLinkedList.addByOrder(hero4);
//		singleLinkedList.addByOrder(hero2);
//        singleLinkedList.addByOrder(hero3);
//        singleLinkedList.addByOrder(hero3);
//		//显示一把
//		singleLinkedList.list();
/*
		//测试修改节点的代码
		HeroNode newHeroNode = new HeroNode(2, "小卢", "玉麒麟~~");
		singleLinkedList.update(newHeroNode);

		System.out.println("修改后的链表情况~~");
		singleLinkedList.list();

		//删除一个节点
		singleLinkedList.del(1);
		singleLinkedList.del(4);
		System.out.println("删除后的链表情况~~");
		singleLinkedList.list();

		//测试一下 求单链表中有效节点的个数
		System.out.println("有效的节点个数=" + getLength(singleLinkedList.getHead()));//2

		//测试一下看看是否得到了倒数第K个节点
		HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 3);
		System.out.println("res=" + res);
*/
    }

    /**
     * 测试双向链表
     */
    public static void testDoubleLinkedList() {
        // 测试
        System.out.println("双向链表的测试");
        // 先创建节点
        DoubleNode hero1 = new DoubleNode(1, "宋江", "及时雨");
        DoubleNode hero2 = new DoubleNode(2, "卢俊义", "玉麒麟");
        DoubleNode hero3 = new DoubleNode(3, "吴用", "智多星");
        DoubleNode hero4 = new DoubleNode(4, "林冲", "豹子头");
        DoubleNode hero5 = new DoubleNode(5, "武松", "打虎");
        // 创建一个双向链表
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);

        doubleLinkedList.list();

        // 修改
//        HeroNode2 newHeroNode = new HeroNode2(4, "公孙胜", "入云龙");
//        doubleLinkedList.update(newHeroNode);
//        System.out.println("修改后的链表情况");
//        doubleLinkedList.list();

        // 删除
//        doubleLinkedList.del(3);
//        System.out.println("删除后的链表情况~~");
//        doubleLinkedList.list();

        // 按顺序添加
//        doubleLinkedList.addByOrder(hero2);
//        doubleLinkedList.addByOrder(hero1);
//        doubleLinkedList.addByOrder(hero4);
//        doubleLinkedList.addByOrder(hero3);
//        doubleLinkedList.addByOrder(hero3);
//        doubleLinkedList.addByOrder(hero5);
//
//        doubleLinkedList.list();
    }

    /**
     * 测试单向环形链表
     */
    public static void testCircleSingleLinkedList() {
        // 测试一把看看构建环形链表，和遍历是否ok
        CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(1);// 加入5个小孩节点
        circleSingleLinkedList.showBoy();

        //测试一把小孩出圈是否正确
        circleSingleLinkedList.countBoy(1, 2, 1); // 2->4->1->5->3
        //String str = "7*2*2-5+1-5+3-3";
    }
}
